﻿#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "libs.h"
#include "pcre2lib.h"

#define MAX_BUF_LEN (1 * 1024 * 1024)
char content[MAX_BUF_LEN];
char chg_content[MAX_BUF_LEN];

FILE* gfp_test;

char default_regex_str[] = \
"[.]*[第卷][0123456789一二三四五六七八九十零〇百千两]*[章回部节集卷].*[.]*";


int got_content(char* path, char* buf, int* len)
{
	//read from local
	FILE* fp = fopen(path, "r");
	if (!fp)
		return -1;
	size_t cnt = fread(buf, 1, MAX_BUF_LEN, fp);
	if (cnt >= MAX_BUF_LEN) {
		printf("file size is larger than %d bytes\n", MAX_BUF_LEN);
	}
	fclose(fp);
	*len = (int)cnt;
	buf[cnt] = 0;

	return 0;
}

static int on_match(char* head, size_t item_offset, size_t item_len, void* ptr)
{
	char* match_ptr = head + item_offset;
	char* tail = match_ptr + item_len;
	char c = *tail;
	*tail = 0;

	FWRITE_IGNORE(match_ptr, (int)strlen(match_ptr));
	printf("\n");
	*tail = c;
	return 0;
}

int match_test(char* buf, long size)
{
	int cnt;
	char* preg = default_regex_str;
	printf("start match_test\nregex: ");
	FWRITE_IGNORE(preg, (int)strlen(preg));
	printf("\n");

	cnt = regex_match(preg, buf, size, 1, on_match, NULL);
	if (cnt <= 0)
		return -1;
	printf("total match %d times\n", cnt);
	printf("end match_test\n\n");
	return 0;
}

void replace_test(char* content, long len)
{
	int ret;
	char* regex_match_str;
	char* regex_replace_str;
	size_t chg_len = MAX_BUF_LEN;

	printf("start replace_test\n");

	//\u003e to >
	regex_match_str = "\\\\u003e?";
	regex_replace_str = ">";

	ret = regex_replace(content, len,
		regex_match_str, regex_replace_str, chg_content, chg_len, 0);
	if (ret > 0) {
		printf("replace_test1: match str: %s\n", regex_match_str);
		printf("match %d times\n\n", ret);
		fprintf(gfp_test, "%s\n", chg_content);

		printf("before replace\n%s\n\n", content);
		printf("after replace\n%s\n\n", chg_content);
	}

	got_content("js.txt", content, &len);
	regex_match_str = "async finderGetCommentDetail\\((\\w+)\\)\\{return(.*?)\\}async";
	regex_replace_str = "async finderGetCommentDetail(\\1){const feedResult=await\\2;var data_object=feedResult.data.object;var media=data_object.objectDesc.media[0];var fetch_body={duration:media.spec[0].durationMs,title:data_object.objectDesc.description,url:media.url+media.urlToken,size:media.fileSize,key:media.decodeKey,id:data_object.id,nonce_id:data_object.objectNonceId,nickname:data_object.nickname,createtime:data_object.createtime,fileFormat:media.spec.map(o => o.fileFormat)};fetch('https://www.httpbin.org/post',{method:'POST',headers:{'Content-Type':'application/json',},body:JSON.stringify(fetch_body)}).then(response=>{console.log(response.ok,response.body)});return feedResult;}async";

	ret = regex_replace(content, len,
		regex_match_str, regex_replace_str, chg_content, chg_len, 0);
	if (ret > 0) {
		printf("replace_test2: match str: %s\n", regex_match_str);
		printf("match %d times\n", ret);
		fprintf(gfp_test, "%s\n", chg_content);

		printf("before replace\n%s\n\n", content);
		printf("after replace\n%s\n\n", chg_content);
	}

	printf("end replace_test\n");
}

/*
pcre2grep.exe  -n "\\u003e?" 1.txt
*/
int main(int argc, char** argv)
{
	int len;
	gfp_test = fopen("1.txt", "w");

	got_content("novel.txt", content, &len);
	match_test(content, len);

	got_content("ori.txt", content, &len);
	replace_test(content, len);

	fclose(gfp_test);
	return 0;
}

